package com.yami.shop.controller;

import com.thoughtworks.xstream.core.SecurityUtils;
import com.yami.shop.bean.dto.AgreeJoinDTO;
import com.yami.shop.bean.dto.GroupNiceDTO;
import com.yami.shop.bean.dto.SetUpGroupAdminDTO;
import com.yami.shop.bean.model.MeltCloudGroup;
import com.yami.shop.bean.model.MeltCloudGroupUserInvite;
import com.yami.shop.bean.model.MeltCloudUser;
import com.yami.shop.bean.vo.MeltCloudGroupUserAdminVO;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.dto.user.GroupAnnouncementDTO;
import com.yami.shop.service.impl.RongYunGroupServiceImpl;
import com.yami.shop.vo.group.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 群组
 */
@RestController
@RequestMapping("/account/im/group")
@Schema(description = "app-钱包IM群组信息")
public class RongYunGroupNewController {

    @Autowired
    private RongYunGroupServiceImpl groupService;

    @PostMapping("/group_create")
    @Operation(summary =  "创建群组")
    public ServerResponseEntity<MeltCloudGroup> create(@RequestBody GroupVo groupVo) throws Exception {
        return groupService.create(groupVo);
    }

    @PostMapping("/group_quit")
    @Operation(summary =  "群主或成员退出群组")
    public ServerResponseEntity<String> quit(@RequestBody @Valid GroupVo groupVo) throws Exception {
        return groupService.quit(groupVo);
    }

    @PostMapping("/group_quit_passive")
    @Operation(summary =  "被动退出群组")
    public ServerResponseEntity<String> quitPassive(@RequestBody @Valid GroupVo groupVo) throws Exception {
        return groupService.quitPassive(groupVo);
    }


    @PostMapping("/group_dismiss")
    @Operation(summary =  "解散群组")
    public ServerResponseEntity<String> dismiss(@RequestBody @Valid GroupVo groupVo) throws Exception {
        return groupService.dismiss(groupVo);
    }

    @PostMapping("/group_update")
    @Operation(summary =  "修改群名称")
    public ServerResponseEntity<String> update(@RequestBody @Valid GroupModelSyncVo groupModelSyncVo) throws Exception {
        return groupService.update(groupModelSyncVo);
    }

    @PostMapping("/group_announcement")
    @Operation(summary =  "发布群公告")
    public ServerResponseEntity<String> updateGroupAnnouncement(@RequestBody @Valid GroupAnnouncementDTO groupModelSyncVo) throws Exception {
        return groupService.updateGroupAnnouncement(groupModelSyncVo);
    }

    @PostMapping("/group_update_user_nick")
    @Operation(summary =  "修改群成员昵称")
    public ServerResponseEntity<String> updateUserGroupNike(@RequestBody @Valid GroupNiceDTO groupModelSyncVo) {
        return groupService.updateUserGroupNike(groupModelSyncVo);
    }

    @GetMapping("/group_info")
    @Operation(summary =  "查询群成员方法", description = "查询群成员方法")
    public ServerResponseEntity<List<MeltCloudUser>> getUserInfo(@RequestParam String groupId) throws Exception {
        return groupService.getInfo(groupId);
    }
    @GetMapping("/group_info_map")
    @Operation(summary =  "查询群成员方法", description = "查询群成员方法")
    public ServerResponseEntity<Map<String,MeltCloudUser>> getUserInfoMap(@RequestParam String groupId, String userId) throws Exception {
        return groupService.getUserInfoMap(groupId, userId);
    }
    @GetMapping("/group_info_search")
    @Operation(summary =  "搜索-群成员方法", description = "查询群成员方法")
    public ServerResponseEntity<List<MeltCloudUser>> getUserInfo(@RequestParam String groupId,String userName,String userId) throws Exception {
        return groupService.getInfo(groupId,userName,userId);
    }
    @GetMapping("/group_info_count")
    @Operation(summary =  "查询群信息和群成员数量", description = "查询群信息和群成员数量")
    public ServerResponseEntity<Map<String,Object>> getUserInfoCount(@RequestParam String groupId,@RequestParam String userId) throws Exception {
        return groupService.getUserInfoCount(groupId,userId);
    }

    @PostMapping("/group_sync")
    @Operation(summary =  "同步用户所属群组方法")
    public ServerResponseEntity<String> sync(@RequestBody @Valid GroupSyncVo syncVo) {
        return groupService.sync(syncVo);
    }


    @PostMapping("/group_join")
    @Operation(summary =  "用户加入指定群组")
    public ServerResponseEntity<String> join(@RequestBody @Valid GroupVo groupVo) throws Exception {
        groupVo.setType(1);
        return groupService.join(groupVo);
    }

    @PostMapping("/ScanGroupJoin")
    @Operation(summary =  "微信扫码加入指定群组")
    public ServerResponseEntity<String> ScanGroupJoin(@RequestBody @Valid GroupScanVo groupScanVo) throws Exception {
        return groupService.ScanGroupJoin(groupScanVo);
    }

    @PostMapping("/setUpAdmin")
    @Operation(summary =  "设置群管理员")
    public ServerResponseEntity<String> setUpAdmin(@RequestBody SetUpGroupAdminDTO setUpGroupAdminDTO){
        return groupService.setUpAdmin(setUpGroupAdminDTO.getUserId(),setUpGroupAdminDTO.getGroupId(), setUpGroupAdminDTO.getCurrentUserId());
    }

    @PostMapping("/cancelAdmin")
    @Operation(summary =  "取消群管理员")
    public ServerResponseEntity<String> cancelAdmin(@RequestBody SetUpGroupAdminDTO setUpGroupAdminDTO){
        return groupService.cancelAdmin(setUpGroupAdminDTO.getUserId(),setUpGroupAdminDTO.getGroupId(), setUpGroupAdminDTO.getCurrentUserId());
    }

    @GetMapping("/getGroupAdminList")
    @Operation(summary =  "查询群管理列表")
    public ServerResponseEntity<List<MeltCloudGroupUserAdminVO>> getGroupAdminList(@RequestParam("groupId")String groupId, String userId){
        return groupService.getGroupAdminList(groupId, userId);
    }


    @GetMapping("/getGroupInviteList")
    @Operation(summary =  "查询加入群聊审核列表")
    public ServerResponseEntity<PageVO<MeltCloudGroupUserInvite>> getGroupInviteList(PageDTO pageDTO, @RequestParam("groupId")String groupId, String userId){
        return groupService.getGroupInviteList(pageDTO, groupId, userId);
    }

    @PostMapping("/isAgreeJoin")
    @Operation(summary =  "审核同意/拒绝加入群聊")
    public ServerResponseEntity<String> isAgreeJoin(@RequestBody AgreeJoinDTO agreeJoinDTO) throws Exception {
        return groupService.isAgreeJoin(agreeJoinDTO);
    }

    @GetMapping("/isInviteBygroupId")
    @Operation(summary =  "是否开启审核")
    public ServerResponseEntity<String> isInviteBygroupId(@RequestParam("groupId")String groupId,@RequestParam("isInvite")String isInvite, @RequestParam String userId){
        return groupService.isInviteBygroupId(groupId, isInvite, userId);
    }

    @GetMapping("/isReviseBygroupId")
    @Operation(summary =  "是否开启群聊名称开关")
    public ServerResponseEntity<String> isReviseBygroupId(@RequestParam("groupId")String groupId, @RequestParam("isRevise")String isRevise, @RequestParam String userId){
        return groupService.isReviseBygroupId(groupId, isRevise, userId);
    }

    @PostMapping("/changeLeader")
    @Operation(summary =  "转让群组")
    public ServerResponseEntity<String> changeLeader(@RequestBody @Valid GroupLeadVo groupLeadVo) {
        return groupService.changeLeader(groupLeadVo);
    }

    @PostMapping("/is_public")
    @Operation(summary =  "设置超级群公开与否")
    public ServerResponseEntity publicByGroupId(@RequestParam Long groupId, @RequestParam String currentUserId) {
        return groupService.publicByGroupId(groupId, currentUserId);
    }

    @GetMapping("/isGroupMember")
    @Operation(summary =  "检查是否群成员")
    public ServerResponseEntity isGroupMember(@RequestParam Long groupId, @RequestParam String userId) throws Exception {
        return groupService.isGroupMember(groupId, userId);
    }
}
